package com.xszx.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xszx.commonutils.JwtUtils;
import com.xszx.servicebase.exceptionhandler.QinXueException;
import com.xszx.ucenter.entity.Member;
import com.xszx.ucenter.entity.dto.LoginDto;
import com.xszx.ucenter.entity.dto.RegisterDto;
import com.xszx.ucenter.mapper.MemberMapper;
import com.xszx.ucenter.service.MemberService;
import com.xszx.ucenter.util.MD5;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 白熊
 * @since 2024-07-24
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LoginDto loginDto) {//登录
        //获取前端传过来的账号密码
        String mobile = loginDto.getMobile();
        String password = loginDto.getPassword();
        //校验参数
        if(StringUtils.isEmpty(mobile) ||
           StringUtils.isEmpty(password)){
            throw new QinXueException(20001,"登录失败");
        }

        //获取会员 通过电话号码查询该用户
        Member member = baseMapper.selectOne(new QueryWrapper<Member>().eq("mobile", mobile));
        if(null == member) {
            throw new QinXueException(20001,"手机号或密码错误");
        }

        //校验密码  MD5加密
        if(!MD5.encrypt(password).equals(member.getPassword())) {
            throw new QinXueException(20001,"手机号或密码错误");
        }
        //校验是否被禁用
        if(member.getIsDisabled()) {
            throw new QinXueException(20001,"error");
        }
        //使用JWT生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    @Override
    public void register(RegisterDto registerDto) {//注册
        //获取注册信息，进行校验 字段不太一样 所以不用 BeanUtils
        String nickname = registerDto.getNickname();
        String mobile = registerDto.getMobile();
        String password = registerDto.getPassword();
        String code = registerDto.getCode();

        //校验参数
        if(StringUtils.isEmpty(mobile) ||
           StringUtils.isEmpty(password) ||
           StringUtils.isEmpty(code)) {
            throw new QinXueException(20001,"请将注册信息填写完整");
        }

        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(mobleCode)) {
            throw new QinXueException(20001,"手机号或密码错误");
        }

        //查询数据库中是否存在相同的手机号码
        Integer count = baseMapper.selectCount(new QueryWrapper<Member>().eq("mobile", mobile));
        if(count.intValue() > 0) {
            throw new QinXueException(20001,"该用户已存在");
        }

        //添加注册信息到数据库
        Member member = new Member();
        member.setNickname(nickname);
        member.setMobile(registerDto.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://qinxue-syx.oss-cn-beijing.aliyuncs.com/avatar/default.jpg");
        this.save(member);
    }

    @Override
    public Member getLoginInfo(String memberId) {//获取登录信从 token 中获取
        Member member = baseMapper.selectById(memberId);
        LoginDto loginDto = new LoginDto();
        BeanUtils.copyProperties(member, loginDto);
        return member;
    }


    @Override
    public Member getByOpenid(String openid) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        Member member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    @Override//统计某一天的注册人数
    public Integer countRegisterByDay(String day) {
        Integer count = baseMapper.selectRegisterCount(day);
        return count;
    }
}
